import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import static java.lang.Thread.sleep;

public class Test {
    /**
     * 多线程案例
     *      2.阻塞队列
     */
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(1000);
        //消费者
        Thread t1 = new Thread(() -> {
                try {
                    while(true) {
                        Integer value = queue.take();
                        System.out.println("t1消费: " + value);
                        sleep(1000);
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
        });
        //生产者
        Thread t2 = new Thread(() -> {
                try {
                    int count = 1;
                    while(true) {
                        queue.put(count);
                        System.out.println("t2生产: " + count);
                        count++;
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }
    public static void main3(String[] args) throws InterruptedException {
        //阻塞队列例子
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(10);
        queue.put("qinBo");
        System.out.println(queue.take());
        System.out.println(queue.take());
    }
    public static void main2(String[] args) {
        /**
         * 生产者 消费者模型(消息队列)
         *      优势:
         *          1.解耦合->降低两者关联性
         *          2.削峰平谷->缓冲区,平衡生产者 消费者处理能力
         */
    }
    public static void main1(String[] args) {
        /**
         * 阻塞队列:
         *      1.当队列满时,继续入队就会阻塞,直到有其它线程取走元素
         *      2.当队列空时,继续出队列就会阻塞,直到有其它线程往队列中插入元素
         */
    }
}
